<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
<script type="text/javascript" src="./js/jquery-1.10.2.js"></script>
<script type="text/javascript">
var save_gubun=0; // 신규입력인지 수정인지 구분 : 0: 신규/ 1: 수정
var oldsabun = ''; // 수정되기 전의 학번 임시 저장

	function init(){
		sabun.value='';
		staffName.value='';
		ipsa_day.value='';
		gwa_code.value='';
		passwd.value='';
		
		jumin1.value='';
		jumin2.value='';
		
		
		
}
	function save(){
		var sabun = $('#sabun').val();
		var name = $('#staffName').val();
		var ipsa_day = $('#ipsa_day').val();
		var gwa_code = $('#gwa_code').val();
		var passwd = $('#passwd').val();
		var gubun = $('input:radio[name=gubun]:checked').val();
		var jumin1 = $('#jumin1').val();
		var jumin2 = $('#jumin2').val();
		var gender = $('input[name=gender]:checked').val();
		
			
		var data={"sabun":sabun, "name":name, "ipsa_day":ipsa_day, "gwa_code":gwa_code,"passwd":passwd,
				"gubun":gubun,"jumin1":jumin1,"jumin2":jumin2,"gender":gender,"oldsabun":oldsabun};
		
		if(sabun ==''){
			alert('사번을 입력하세요');
			return;
		}
		if(name == ''){
			alert('이름을 입력하세요');
			return;
		}
		if(ipsa_day==''){
			alert('입사일 입력하세요');
			return;
		}
		if(gwa_code==''){
			alert('과코드를 입력하세요');
			return;
		}
		if(passwd==''){
			alert('비밀번호를 입력하세요');
			return;
		}
		if(gubun==null){
			alert('구분을 선택하세요');
			return;
		}
		   if(jumin1==''){
			alert('주민번호 앞자리를 입력하세요');
			return;
		}
		if(jumin2==''){
			alert('주민번호 뒷자리를 입력하세요');
			return;
		}
		if(gender==null){
			alert('성별을 선택하세요');
			return;
		}
		
		if(save_gubun!='1'){
		
			
		$.ajax({
			type:"post",
			data:data,
			url:"checkStaff.do",
			dataType:"json",
			success:function(data1){
				if(data1.result=='exists')
				{
					alert('해당 사번이 이미 존재 합니다.');
				}else
				{
					$("#output").empty();
					var head = "<tr><td></td><td>사번</td><td>성명</td><td>입사일</td><td>과코드</td><td>비밀번호</td><td>구분</td><td>주민번호</td><td>성별</td></tr>";
					$("#output").append(head);
					
					$(data1).each(function(index,item){
						output+="<tr>";
						output+='<td><input type="button" id="btnUpdate" value="수정" onclick="update(\''+item.sabun+'\',\''+item.name+'\',\''+item.ipsa_day+'\',\''+item.gwa_code+'\',\''+item.passwd+'\',\''+item.gubun+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
						output+="<td><input type=checkbox name=s_sabun value="+item.sabun+">" + item.sabun +"</td>";
						output+="<td>"+item.name+"</td>";
						output+="<td>"+item.ipsa_day+"</td>";
						output+="<td>"+item.gwa_code+"</td>";
						output+="<td>"+item.passwd+"</td>";
						output+="<td>"+item.gubun+"</td>";
						output+="<td>"+item.jumin1+"-"+item.jumin2+"</td>";
						output+="<td>"+item.gender+"</td>";
						output+="</tr>";
					$("#output").append(output);
					output = '';
					});
					$('select').find('option:first').attr('selected', 'selected');
					$('input:radio[name=gender]').removeAttr('checked');
					$('input:radio[name=gubun]').removeAttr('checked');
					init();
				}
				 
			},
			error:function(e){
				alert('에러가 났습니다.');
			}
				
			});
		
	}else{
	
	var data={"sabun":sabun, "name":name, "ipsa_day":ipsa_day, "gwa_code":gwa_code,"passwd":passwd,
			"gubun":gubun,"jumin1":jumin1,"jumin2":jumin2,"gender":gender,"oldsabun":oldsabun};
	
	
	
	$.ajax({
		type:"post",
		data:data,
		url:"updateStaff.do",
		dataType:"json",
		success:function(data2)
		{
			$("#output").empty();
			var head = "<tr><td></td><td>사번</td><td>성명</td><td>입사일</td><td>과코드</td><td>비밀번호</td><td>구분</td><td>주민번호</td><td>성별</td></tr>";
			
			$("#output").append(head);
			
		$(data2).each(function(index,item){
			output+="<tr>";
			output+='<td><input type="button" id="btnUpdate" value="수정" onclick="update(\''+item.sabun+'\',\''+item.name+'\',\''+item.ipsa_day+'\',\''+item.gwa_code+'\',\''+item.passwd+'\',\''+item.gubun+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
			output+="<td><input type=checkbox name=s_sabun value="+item.sabun+">" + item.sabun +"</td>";
			output+="<td>"+item.name+"</td>";
			output+="<td>"+item.ipsa_day+"</td>";
			output+="<td>"+item.gwa_code+"</td>";
			output+="<td>"+item.passwd+"</td>";
			output+="<td>"+item.gubun+"</td>";
			output+="<td>"+item.jumin1+"-"+item.jumin2+"</td>";
			output+="<td>"+item.gender+"</td>";
			output+="</tr>";
		$("#output").append(output);
		output = '';
		});
		$('select').find('option:first').attr('selected', 'selected');
		$('input:radio[name=gender]').removeAttr('checked');
		$('input:radio[name=gubun]').removeAttr('checked');
		init();
		}
		
	});
	save_gubun = 0; //업데이트후 다시 save_gubun = 0으로 셋팅
	
	}
	
	}
			
function del(){
	var s_sabun = $('input[name=s_sabun]:checked');
	var arr="";
	for(var i=0;i<s_sabun.length;i++)
	{
		
			if(i>0)
			{
				arr+= ",";
			}
			arr+= s_sabun[i].value;
		}
	
	
	var data={"checkedSabun":arr,"gwa_code":$('#gwa_code').val()};
	$.ajax({
		type:"post",
		data:data,
		url:"deleteStaff.do",
		dataType:"json",
		success:function(data1){
			$("#output").empty();
			var head = "<tr><td></td><td>사번</td><td>성명</td><td>입사일</td><td>과코드</td><td>비밀번호</td><td>구분</td><td>주민번호</td><td>성별</td></tr>";
			   $('#output').append(head);
			   $(data1).each(function(index,item){
					output+="<tr>";
					output+='<td><input type="button" id="btnUpdate" value="수정" onclick="update(\''+item.sabun+'\',\''+item.name+'\',\''+item.ipsa_day+'\',\''+item.gwa_code+'\',\''+item.passwd+'\',\''+item.gubun+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
					output+="<td><input type=checkbox name=s_sabun value="+item.sabun+">" + item.sabun +"</td>";
					output+="<td>"+item.name+"</td>";
					output+="<td>"+item.ipsa_day+"</td>";
					output+="<td>"+item.gwa_code+"</td>";
					output+="<td>"+item.passwd+"</td>";
					output+="<td>"+item.gubun+"</td>";
					output+="<td>"+item.jumin1+"-"+item.jumin2+"</td>";
					output+="<td>"+item.gender+"</td>";
					output+="</tr>";
				$("#output").append(output);
			   output = '';	   
	});
},
error:function(e){
	}
});
}
	
	
	function update(obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9){
		
		
		if(obj9=='여')
		{
			$('input:radio[name=gender]:input[value=F]').prop('checked',true);
			
		}else
		{
			$('input:radio[name=gender]:input[value=M]').prop('checked',true);
		}
		
			
		if(obj6=='일반직원'){
			$('input:radio[name=gubun]:input[value=1]').prop('checked',true);
		}else{
			$('input:radio[name=gubun]:input[value=2]').prop('checked',true);
		}
		
		save_gubun = 1; // 수정시 학번 중복체크를 하지 않도록 플래그 값을 1로 변경
		oldsabun = obj1;
		
		sabun.value=obj1;
		staffName.value=obj2;
		ipsa_day.value=obj3;
		gwa_code.value=obj4;
		$("#gwalist").val(obj4);
		passwd.value=obj5;
		
		jumin1.value=obj7;
		jumin2.value=obj8;
		gender.value=obj9;
	}
	
	function goback(){
		 location.href = "staffMain.do";
	}
	function selectGwa(gwalist){
		var gwacode=gwalist.options[gwalist.selectedIndex].value;
		
		
		
		$('#gwa_code').val(gwacode);
			
		
	}
	
	
</script>
</head> 
<body>
<!--  <div class="command"> -->
<table class="table2">
	<tr>
 		<th><spring:message code="staff.form.sabun"/></th>
 		<td><input type="text" id="sabun" /></td>
 		<th><spring:message code="staff.form.passwd"/></th>
 		<td><input type="text" id="passwd" /></td>
 	</tr>
 	<tr>
 		<th><spring:message code="staff.form.name"/></th>
 		<td><input type="text" id="staffName"  /></td>
 		<th><spring:message code="staff.form.gubun"/></th>
 		<td>
 			<input type="radio" name="gubun" value="1" />일반직원 
 			<input type="radio" name="gubun" value="2"/>교수
 		</td>
 	</tr>
 	<tr>
 		<th><spring:message code="staff.form.ipsa_day"/></th>
 		<td><input type="date" id="ipsa_day"  /></td>
 		<th><spring:message code="staff.form.jumin"/></th>
 		<td><input type="text" id="jumin1" />-<input type="text" id="jumin2" /></td>
 	</tr>
 	<tr>
 		<th><spring:message code="staff.form.gwa_code"/></th>
 		<td>
 			<input type="hidden" id="gwa_code">
 				<select id="gwalist" name="gwalist" onChange="selectGwa(this)">
					<option selected="selected">과선택</option>
				<c:forEach var="gwalist" items="${gwalist}">
					<option value="${gwalist.code}">${gwalist.code}(${gwalist.name})</option>
				</c:forEach>
				</select>
 		</td>
 		<th><spring:message code="staff.form.gender"/></th>
 		<td>
 			<input name="gender" type="radio" value="M" />남성
 			<input name="gender" type="radio" value="F" />여성<br>
		</td> 
 	</tr>
 		
  	<tr><td colspan="2">
 	<input type="button" id="btnSave" value="저장" onclick="save()" />
	<input type="button" id="btnDel" name="sabun" value="삭제" onclick="del()" /> 
	<input type="button" id="btnExit" value="종료" onclick="goback()" />
	</td></tr>
 </table>
 <h2>직원 목록</h2>
 <div id="staffMng" class="defTable">
 	<table id="output">
 		<tr id="thfix">
 			<td></td>
 			<td>사번</td>
 			<td>성명</td>
 			<td>입사일</td>
 			<td>과코드</td>
 			<td>비밀번호</td>
 			<td>구분</td>
 			<td>주민번호</td>
 			<td>성별</td>
		</tr>
		<c:forEach var="list" items="${staffList}">
		<tr>
			<td>
				<input type="button" id="btnUpdate" value="수정" onclick="update('${list.sabun}','${list.name}','${list.ipsa_day}','${list.gwa_code}','${list.passwd}','${list.gubun}','${list.jumin1}','${list.jumin2 }','${list.gender}')"/>
			</td>
			<td>
				<input type="checkbox" name="s_sabun" value="${list.sabun }"/>${list.sabun }
			</td>
			<td>
				${list.name }
			</td>
			<td>
				${list.ipsa_day }
			</td>
			<td>
				${list.gwa_code }
			</td>
			<td>
				${list.passwd }
			</td>
			<td>
				${list.gubun}
			</td>
			<td>
				${list.jumin1 }-${list.jumin2 }
			</td>
			<td>
				${list.gender }
			</td>
		</tr>
		</c:forEach> 			
 	</table>
  </div>
</body>
</html>


 
